package com.stms.service.impl;

import com.stms.dao.TrucksRepository;
import com.stms.pojo.Staff;
import com.stms.pojo.Trucks;
import com.stms.service.TrucksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class TrucksServiceImpl implements TrucksService {
    @Autowired
    public TrucksRepository trucksRepository;

    @Override
    public boolean addTrucks(Trucks trucks) {
        if(trucksRepository.findByLicense(trucks.getLicense())!=null){
            return false;
        }else{
            if(trucksRepository.save(trucks)!=null) {
                return true;
            }else{
                return false;
            }
        }
    }

    @Override
    public boolean editTrucks(Trucks trucks) {
        if(trucksRepository.save(trucks)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void deleteBatch(String trucksIds) {
        String[] trucksStr = trucksIds.split(",");
        List<Integer> list = new ArrayList<Integer>();

        for(String str:trucksStr){
            list.add(Integer.valueOf(str));
        }
        trucksRepository.deleteBatch(list);
    }

    @Override
    public List<Trucks> findAllLicense() {
        return trucksRepository.findAll();
    }

    @Override
    public Page<Trucks> fuzzyQuery(Integer pageNum, Integer pageSize, String direction, Trucks trucks) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }
        if(direction==null){
            direction = new String("DESC");
        }

        //排序、分页初始化
        Sort.Direction direc = direction.equals("DESC")?Sort.Direction.DESC:Sort.Direction.ASC;
        Pageable pageable = PageRequest.of(pageNum,pageSize, direc,"id");

        Page<Trucks> page = trucksRepository.findAll(new Specification<Trucks>() {
            @Override
            public Predicate toPredicate(Root<Trucks> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //创建条件集合
                List<Predicate> list = new ArrayList<>();
                if (trucks == null) {
                    return null;
                }
                if (trucks.getId() != null && !trucks.getId().equals("")) {
                    list.add(cb.equal(root.get("id"), trucks.getId()));
                }
                if (trucks.getLicense() != null && !trucks.getLicense().equals("")) {
                    list.add(cb.like(root.get("license").as(String.class), "%" + trucks.getLicense() + "%"));
                }
                if (trucks.getStatus() != null && !trucks.getStatus().equals("")) {
                    list.add(cb.equal(root.get("status"), trucks.getStatus()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
        return page;
    }

    @Override
    public Page<Trucks> findByPage(Integer pageNum, Integer pageSize) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Trucks> page = trucksRepository.findAll(of);
        return page;
    }
}
